#!/usr/bin/perl

use strict;

use lib qw( lib t/lib );
use SQLiteTest;
use DataValidatorSchema;
use Data::Dumper;
use Test::More tests => 5;

my $schema = SQLiteTest::get_test_schema( "DataValidatorSchema" );
my $id = 10;

ok( test_missing_method(), "Test missing named method" );
ok( test_static_named_method(), "Test static named methods" );
ok( test_runtime_named_method(), "Test runtime named methods" );
ok( test_merge_methods(), "Test merge of static and runtime methods" );
ok( test_column_class(), "Test column classes" );



sub test_missing_method {
    my $test = $schema->resultset( 'Bar' )->first;
    
    eval {
        $test->set_runtime_validation_methods( {
            test_dynamic => sub { return 1 }
        } );
        $test->update_validated( {
            name => 'foo ok',
            id   => $id++
        }, qw/ test_no_method / );
    };
    if ( my $e = $@ ) {
        return $e->message eq "No such named method 'no_such_method' in constraint for column 'id' in profile 'test_no_method'";
    }
    
    return 0;
}

sub test_static_named_method {
    my $test = $schema->resultset( 'Bar' )->first;
    
    eval {
        $test->update_validated( {
            name => 'ok foo',
            id   => $id++
        }, qw/ test_static_method / );
    };
    return $@ ? 0 : 1;
}

sub test_runtime_named_method {
    my $test = $schema->resultset( 'Bar' )->first;
    
    eval {
        
        # set runtime ..
        # $test->set_runtime_validation_methods( {
        #     test_runtime => sub { return 1 }
        # } );
        $schema->resultset( 'Bar' )->set_runtime_validation_methods( {
            test_runtime => sub { return 1 }
        } );
        
        # test for update
        $test->update_validated( {
            name => 'ok foo',
            id   => $id++
        }, qw/ test_runtime_method / );
        
        # test for second update
        $test->update_validated( {
            name => 'ok foo',
            id   => $id++
        }, qw/ test_runtime_method / );
        
        # test for updateall
        $schema->resultset( 'Bar' )->search( undef, { limit => 2 } )->update_all( {
            name => 'ok foo',
        }, qw/ test_runtime_method / );
        
        
        # test for create
        $schema->resultset( 'Bar' )->set_runtime_validation_methods( {
            test_runtime => sub { return 1 }
        } );
        $schema->resultset( 'Bar' )->create_validated( {
            name => 'ok foo',
            id   => $id++
        }, qw/ test_runtime_method / );
        
        warn Dumper [ $schema->resultset( 'Bar' )->_runtime_validation_methods ];
    };
    warn Dumper [ $@ ] if $@;
    return $@ ? 0 : 1;
}

sub test_merge_methods {
    my $test = $schema->resultset( 'Bar' )->first;
    
    eval {
        $test->set_runtime_validation_methods( {
            test_runtime => sub { return 1 }
        } );
        $test->update_validated( {
            name => 'ok foo',
            id   => $id++
        }, qw/ test_both_metod / );
    };
    return $@ ? 0 : 1;
}

sub test_column_class {
    my $test = $schema->resultset( 'Bar' )->first;
    
    eval {
        $test->update_validated( {
            name => 'lala',
        } );
    };
    return 0 if $@;
    
    eval {
        $test->update_validated( {
            name => 'yadda',
        } );
    };
    return 1 if $@;
}


1;
